﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Entity;
using Business.Abstract;
using Business.Service;
using PresentationMVC.Models;
using Business.Common;

namespace PresentationMVC.Controllers
{
    public class JobController : Controller
    {
        IJobBookingService jobBookingSrv;
        public JobController()
        {
            jobBookingSrv = new JobBookingServiceImpl();
        }

        public ActionResult Index()
        {
            JobBooking jobBooking = new JobBooking();
            JobBookingModel model = new JobBookingModel();
            model.JobBooking = jobBooking;
            return View(model);
        }

       
        public ActionResult CarParkingEntryRow()
        {
            return PartialView("ParkingDetailEditRow");
        }

        [HttpPost]
        public ActionResult Save(JobBookingModel model)
        {
            JobBooking jobBooking = model.JobBooking;
            
            jobBooking.Customer = new Customer { ID = model.SelectedCustomerId};
            jobBooking.Staff = new Staff { ID = model.SelectedStaffId};
            jobBooking.CreatedDate = DateTime.Now;

            return SaveOrUpdate(model);
        }

        public ActionResult ViewJobs()
        {
            JobBooking jobBooking = new JobBooking();
            JobBookingModel model = new JobBookingModel();
            model.JobBooking = jobBooking;
            model.AllJobBooking = jobBookingSrv.GetAllJobBookings();
            return View(model);
        }

        [HttpGet]
        public ActionResult ReloadGrid()
        {
            IJobBookingService jobBookingSrv = new JobBookingServiceImpl();
            JobBookingModel model = new JobBookingModel();
            model.AllJobBooking = jobBookingSrv.GetAllJobBookings();
            this.ViewData.Model = model;
            return PartialView("ViewGrid", model);
        }

        [HttpGet]
        public ActionResult Edit(long id)
        {
            JobBooking jobBooking = jobBookingSrv.GetJobBookingById(id);
            JobBookingModel model = new JobBookingModel();
            model.JobBooking = jobBooking;
            model = GetJobDataModel(model);
            return View("Index", model);
        }

        /// <summary>
        /// Delete the customer 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Delete(long id)
        {
            IJobBookingService jobBookingSrv = new JobBookingServiceImpl();
            JobBooking jobBooking = jobBookingSrv.GetJobBookingById(id);
            //jobBookingSrv.DeleteJobBooking(jobBooking);
            jobBooking.IsDeleted = true;
            jobBookingSrv.UpdateJobBooking(jobBooking);

            ViewData["DELETE_MSG"] = "Successsfully deleted the record.";
            JobBookingModel model = new JobBookingModel();
            model.AllJobBooking = jobBookingSrv.GetAllJobBookings();
            this.ViewData.Model = model;
            return PartialView("ViewGrid");
        }

        [HttpPost]
        public ActionResult GridviewSearch(JobBookingModel model)
        {
            Session[Constants.JOB_SEARCH_SESSION] = model;
            this.ViewData.Model = GetJobDataModel(model);
            return PartialView("ViewGrid");
        }

        private JobBookingModel GetJobDataModel()
        {
            return GetJobDataModel(null);
        }

        private JobBookingModel GetJobDataModel(JobBookingModel model)
        {
            IList<JobBooking> lstJobBooking = new List<JobBooking>();
            if (Session[Constants.JOB_SEARCH_SESSION] == null)
            {
                lstJobBooking = jobBookingSrv.GetAllJobBookings();
            }
            else
            {
                JobBookingModel search = Session[Constants.JOB_SEARCH_SESSION] as JobBookingModel;
                lstJobBooking = jobBookingSrv.GetAllJobBooking(model.JobBooking,model.SelectedCustomerId,model.SelectedStaffId,model.From, model.To);
            }
            model.JobBooking = model.JobBooking == null ? new JobBooking() : model.JobBooking;
            model.AllJobBooking = lstJobBooking;
            return model;
        }

        /// <summary>
        /// Base on the customer object it create a new customer record or update already existing record.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private ActionResult SaveOrUpdate(JobBookingModel model)
        {
            JobBooking jobBooking = model.JobBooking;
           
            if (ModelState.IsValid)
            {
                try
                {
                    Refund refund = jobBooking.Refund;
                    refund.UpdatedDate = DateTime.Now;
                    refund.CreatedDate = DateTime.Now;
                        
                    if (jobBooking.ID > 0)
                    {
                        JobBooking tmpJobBooking = jobBookingSrv.GetJobBookingById(jobBooking.ID);
                        tmpJobBooking.JobDateTime = jobBooking.JobDateTime;
                        tmpJobBooking.Note = jobBooking.Note;
                        tmpJobBooking.ToAddressLine1 = jobBooking.ToAddressLine1;
                        tmpJobBooking.ToAddressLine2 = jobBooking.ToAddressLine2;
                        tmpJobBooking.ToCity = jobBooking.ToCity;
                        tmpJobBooking.ToCounty = jobBooking.ToCounty;
                        tmpJobBooking.ToPostcode = jobBooking.ToPostcode;
                        tmpJobBooking.UpdatedDate = jobBooking.UpdatedDate;
                        tmpJobBooking.WaitingCharge = jobBooking.WaitingCharge;
                        tmpJobBooking.Fare = jobBooking.Fare;
                        tmpJobBooking.FromAddressLine1 = jobBooking.FromAddressLine1;
                        tmpJobBooking.FromAddressLine2 = jobBooking.FromAddressLine2;
                        tmpJobBooking.FromCity = jobBooking.FromCity;
                        tmpJobBooking.FromPostcode = jobBooking.FromPostcode;
                        tmpJobBooking.CarParkDetails = jobBooking.CarParkDetails;

                        tmpJobBooking.BookingType = jobBooking.BookingType;
                        tmpJobBooking.Customer = jobBooking.Customer;
                        tmpJobBooking.PassengerDetails = jobBooking.PassengerDetails;
                        tmpJobBooking.Staff = jobBooking.Staff;
                        tmpJobBooking.Status = jobBooking.Status;
                        tmpJobBooking.Refund = jobBooking.Refund;

                        jobBookingSrv.UpdateJobBooking(tmpJobBooking);
                        ViewData["MSG"] = "Successfully updated the details.";
                    }
                    else
                    {
                        jobBookingSrv.Save(jobBooking);
                        ViewData["MSG"] = "Successfully saved the details.";
                    }

                    ModelState.Clear();
                    jobBooking = new JobBooking();
                    model.JobBooking = jobBooking;

                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", "Error" + ex.ToString());
                }
            }

            model.AllJobBooking = jobBookingSrv.GetAllJobBookings();
            return RedirectToAction("Index"); ;
        }
    }
}
